﻿using System;
using System.Collections.Generic;
using UserGroupsClassLIB;
using System.Data.SqlClient;
using UtilityClassLIB;

namespace UserGroupsServiceLibrary
{

    public class UserGroupsService : IUserGroupsService
    {
        UtilityClass utility_obj = new UtilityClass();//creates utility class object
        String connectionString = UtilityClass.ConnectionString();//gets connection string from utility class

        public List<UserGroupClass> GetAllUserGroup()
        {
            List<UserGroupClass> userGroupList = new List<UserGroupClass>();


            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string queryString = "SELECT userID,groupID, userGroupDisabled FROM [dbo].[tbUsergrp]";
                try
                {

                    connection.Open();

                    SqlCommand command = new SqlCommand(queryString, connection);

                    SqlDataReader reader = command.ExecuteReader();

                    while (reader.Read())
                    {
                        UserGroupClass aUserGroup = new UserGroupClass();

                        aUserGroup.UserID = reader.GetInt32(0);
                        aUserGroup.GroupID = reader.GetInt32(1);
                        aUserGroup.Disabled = reader.GetString(2);
                            

                        userGroupList.Add(aUserGroup);
                    }
                    connection.Close();

                }

                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                return userGroupList;
            }
        }

        public bool AddUserGroup(UserGroupClass obj)
        {
            bool success = false;

            try
            {

                using (SqlConnection conn = new SqlConnection(connectionString))
                {

                    string sqlString = "insert into [dbo].[tbUsergrp](userID, grpID, userGroupDisabled) values(@userID, @grpID, @userGroupDisabled)";
                    conn.Open();
                    using (SqlCommand command = new SqlCommand(sqlString, conn))
                    {
                        command.Parameters.AddWithValue("@userID", obj.UserID);
                        command.Parameters.AddWithValue("@grpID", obj.GroupID);
                        command.Parameters.AddWithValue("@userGroupDisabled", obj.Disabled);

                        int rows = command.ExecuteNonQuery();

                        if (rows == 1)
                        {
                            success = true;
                        }

                        //rows number of record got updated
                    }
                }
            }
            catch (SqlException ex)
            {
                //Log exception
                //Display Error message
                Console.WriteLine(ex.ToString());
            }

            return success;
        }




    }
}
